/**
 * 
 */
package com.rocky.project.action.model;

import java.util.LinkedList;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.admin.Area;
import com.rocky.project.model.family.Family;
import com.rocky.project.model.family.Person;
import com.rocky.project.util.BeanUtil;
import com.rocky.project.util.WebUtil;

/**
 * @author chen
 *
 */
public class PersonAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Person person;
	private Family family;

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	@Override
	public String read() throws Exception {
		java.util.List<Object> paramsvalues = new LinkedList<Object>();
		String queryHql = "SELECT m FROM Person AS m WHERE m.deleted = false   ";		
		String keywordFilterValue = WebUtil.getParaValue("keywordFilterValue");
		String id = WebUtil.getParaValue("id");
		String familyid = WebUtil.getParaValue("familyid");		
		if(keywordFilterValue!=null && !keywordFilterValue.equals("") ){
			queryHql +="    and (m.name like ?   or m.cardnumber like ?  ) ";
			paramsvalues.add("%"+keywordFilterValue+"%");
			paramsvalues.add("%"+keywordFilterValue+"%");			
		}	
		if(id!=null && !id.equals("") ){
			queryHql +="    and m.id = ?   ";			
			paramsvalues.add(Long.valueOf(id));			
		}	
		if(StringUtils.isNotEmpty(familyid)){
			queryHql +="    and m.family.id = ?   ";			
			paramsvalues.add(Long.valueOf(familyid));			
		}		
		queryHql = queryHql + "    order by m.id desc";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),getStart(), getPageSize());
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());
		
		this.ajaxResultMap.put("data", this.pageRecords);
		this.ajaxResultMap.put("rowCount", this.rowCount);
		
		log.info("PersonAction:read.action->"+this.rowCount);
		if(StringUtils.isNotEmpty(this.suc)){
			return this.suc;
		}
		return "personread";
	}	
	
	@Override
	public String readOne() throws Exception {
		person = (Person) baseService.get(Person.class, Long.valueOf(id));
		return super.readOne();
	}
	@Override
	public String createProcess() throws Exception {		
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		String areaid = obj.getString("areaid");		
		String householderRelation = obj.getString("householderRelation");		
		String familyid = obj.getString("familyid");	
		person = (Person) JSONObject.toBean( obj, Person.class );	
		WebUtil.init4create(person);
		if(!StringUtils.isEmpty(areaid)){
			Area area = new Area();
			area.setId(Long.valueOf(areaid));
			person.setArea(area);
		}	
		if(!StringUtils.isEmpty(familyid)){//先判断农户是否存在，不存在则新建农户
			family =(Family) baseService.get(Family.class, Long.valueOf(familyid));	
		}else{			
			family = new Family();
			family.setAddress(person.getAddress());
			family.setFamilycode(obj.optString("familycode","请在家庭信息模块中维护家庭编号"));
			family.setArea(person.getArea());
			baseService.create(family);					
		}
		person.setFamily(family);
		baseService.create(person);
		if(householderRelation!=null&&householderRelation.equals("0")){
			family.setHouseholder(person);
			family.setHouseholdername(person.getName());
			baseService.update(family);			
		}
		return ajaxResult;
	}
	@Override
	public String updateProcess() throws Exception {
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		String areaid = obj.getString("areaid");
		String householderRelation = obj.getString("householderRelation");		
//		String familycode =obj.optString("familycode", "请在家庭信息模块中维护家庭编号");		
		String familyid = obj.getString("familyid");			
		person = (Person) JSONObject.toBean( obj, Person.class );
		Person objDb =(Person) baseService.get(Person.class, person.getId());	
		String householderRelationOld = objDb.getHouseholderRelation();
		BeanUtil.copyProperties(person,objDb);
		WebUtil.init4modify(objDb);	
		if(!StringUtils.isEmpty(areaid)){
			Area area = new Area();
			area.setId(Long.valueOf(areaid));
			objDb.setArea(area);
		}
		if(!StringUtils.isEmpty(familyid)){
			family =(Family) baseService.get(Family.class, Long.valueOf(familyid));	
		}else{//没有所属家庭报错
			this.addActionMessage("没有所属家庭,请选择所属家庭！");
			log.error("#没有所属家庭报错#：familyid");
			return ERROR;
		}		
		if(householderRelation!=null&&householderRelation.equals("0")){			
			if(householderRelationOld!=null && (householderRelationOld.equals("户主")||householderRelationOld.equals("本人"))){//是原来家庭的户主
				Family familyOld = objDb.getFamily();
				familyOld.setHouseholder(null);
				familyOld.setMemo("原户主已更改所属家庭");
				baseService.update(familyOld);	
			}
			family.setHouseholder(objDb);
			family.setHouseholdername(objDb.getName());
			baseService.update(family);		
		}
		objDb.setFamily(family);
		baseService.update(objDb);
		return ajaxResult;
	}	
	
	@Override
	public String deleteProcess() throws Exception {		
		deleteProcess(Person.class);
		ServletActionContext.getResponse().setContentType("text/plain charset=utf-8");
		ServletActionContext.getResponse().getWriter().write("{success:true}");	
		return super.deleteProcess();
	}	

	/*
	@Override
	public String deleteProcess() throws Exception {
		deleteProcess(Person.class);
		return super.deleteProcess();
	}*/

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}	

}
